package com.yuchengtech.test.pack.action;

import java.io.File;
import java.util.LinkedList;

import com.wox.action.JsonData;
import com.wox.action.WoxAction;
import com.yuchengtech.fileUtil.FileIo;
import com.yuchengtech.fileUtil.FileUtil;

public class PackFileListAction extends WoxAction {

	private String webInfDir;
	private	String packDir;
	private String listFile;
	private LinkedList<String> failedFiles;



	@Override
	public String excute() throws Exception {

		JsonData data=getJsonData();
		failedFiles=new LinkedList<String>();
		
		if(webInfDir!=null&&webInfDir.trim().length()>0
				&&packDir!=null&&packDir.trim().length()>0
				&&listFile!=null&&listFile.trim().length()>0)
		{
			File listFl=new File(listFile);
			if(listFl.exists()&&listFl.isFile())
			{
				String listStr=FileIo.readFileAsString(listFl);
				String[] srcs=listStr.split("\r\n");
				for(String src:srcs)
				{
					if(src.trim().length()!=0)
					{
						boolean b=FileUtil.copyFile(FileUtil.contactPath(webInfDir,src),
										  FileUtil.contactPath( packDir,src));
						if(!b)
							failedFiles.add(src);
					}
				}
				StringBuilder builder=new StringBuilder("列表文件:"+listFile+" 已打包完成!<br/>");
				int i=1;
				if(failedFiles.size()!=0)
				{
					builder.append("下列文件有误:<br>");
					for (String str2 : failedFiles) {
						builder.append(i+".").append(str2+"<br/>");
						i++;
					}
				}
				data.setMessage(builder.toString());
	
			}else
				data.setMessage("Sorry(-_-),读取列表文件失败!");
		}else
		{
			data.setMessage("Sorry(-_-),参数有误!");
		}
		return JSONBACK;
	}
	
	
	public void setWebInfDir(String webInfDir) {
		this.webInfDir = webInfDir;
	}


	public void setPackDir(String packDir) {
		this.packDir = packDir;
	}


	public void setListFile(String listFile) {
		this.listFile = listFile;
	}
}
